package com.fengxing.ams.web.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fengxing.ams.pub.manager.intf.IConfigManager;
import com.fengxing.ams.pub.util.StringUtil;
import com.fengxing.ams.web.action.view.OrderApplyVO;
import com.fengxing.ams.web.action.view.OrderCheckVO;
import com.fengxing.ams.web.action.view.OrderForCheckVO;
import com.fengxing.ams.web.action.view.OrderQueryVO;
import com.fengxing.ams.web.action.view.ResponseVO;
import com.fengxing.ams.web.action.view.TimeLineQueryInput;
import com.fengxing.ams.web.common.IContextManager;
import com.fengxing.ams.web.facade.intf.INotifyFacade;
import com.fengxing.ams.web.facade.intf.IOrderFacade;
import com.fengxing.ams.web.facade.intf.IStudentFacade;
import com.fengxing.ams.web.facade.intf.ISysManagerFacade;
import com.fengxing.ams.web.facade.intf.IUserFacade;
import com.fengxing.ams.web.facade.mouble.company.CompanyVO;
import com.fengxing.ams.web.facade.mouble.order.OrderHistoryVO;
import com.fengxing.ams.web.facade.mouble.student.StudentSCMVO;
import com.fengxing.ams.web.facade.mouble.user.UserVO;

@Controller
@RequestMapping("order")
@Transactional
public class OrderAction {
	
	private Log logger = LogFactory.getLog(OrderAction.class);
	
	@Autowired
	private IUserFacade userFacade;
	@Autowired
	private INotifyFacade notifyFacade;
	@Autowired
	private IContextManager contextManager;
	@Autowired
	private IConfigManager configManager;
	@Autowired
	private ISysManagerFacade sysManagerFacade;
	@Autowired
	private IOrderFacade orderFacade;
	@Autowired
	private IStudentFacade studentFacade;
	
	private static final Random r = new Random();
	
	@RequestMapping(value = "/submit.do", method = RequestMethod.POST)
	public @ResponseBody ResponseVO studentProfile(@RequestBody OrderApplyVO orderVO) {
		//先检查权限
		ResponseVO vo = new ResponseVO();
		UserVO loginedUser = this.contextManager.getUserVO();
		if (loginedUser == null) {
			vo.setSuccess(false);
			vo.setMessage("需要重新登录");
			return vo;
		}
		if (!loginedUser.getEmail().equals(orderVO.getProfile().getId())) {
			vo.setSuccess(false);
			vo.setMessage("你没有操作该资源的权限");
			return vo;
		}
		if (!loginedUser.getRoleIdList().contains(Long.valueOf(4L))) {
			vo.setSuccess(false);
			vo.setMessage("你没有操作该资源的权限");
			return vo;
		}
		//补全一些信息
		StudentSCMVO student = orderVO.getProfile();
		CompanyVO company = orderVO.getCompany();
		
		this.studentFacade.orderApply(student, company);
		
		vo.setMessage("请求成功！");
		return vo;
	}
	
	@RequestMapping(value = "/reapply.do", method = RequestMethod.POST)
	public @ResponseBody ResponseVO studentOrderReApply(@RequestBody OrderApplyVO orderVO) {
		//先检查权限
		ResponseVO vo = new ResponseVO();
		UserVO loginedUser = this.contextManager.getUserVO();
		if (loginedUser == null) {
			vo.setSuccess(false);
			vo.setMessage("需要重新登录");
			return vo;
		}
		if (!loginedUser.getEmail().equals(orderVO.getProfile().getId())) {
			vo.setSuccess(false);
			vo.setMessage("你没有操作该资源的权限");
			return vo;
		}
		if (!loginedUser.getRoleIdList().contains(Long.valueOf(4L))) {
			vo.setSuccess(false);
			vo.setMessage("你没有操作该资源的权限");
			return vo;
		}
		//补全一些信息
		StudentSCMVO student = orderVO.getProfile();
		CompanyVO company = orderVO.getCompany();
		
		this.studentFacade.orderReApply(student, company);
		
		vo.setMessage("请求成功！");
		return vo;
	}
	
	@RequestMapping(value = "/queryForCheckList.do", method = RequestMethod.POST)
	public @ResponseBody ResponseVO queryForCheck() {
		//先检查权限
		ResponseVO vo = new ResponseVO();
		UserVO loginedUser = this.contextManager.getUserVO();
		if (loginedUser == null) {
			vo.setSuccess(false);
			vo.setMessage("需要重新登录");
			return vo;
		}
		//检查角色是否为管理员角色
		if (!(loginedUser.getRoleIdList().contains(Long.valueOf(5L)) || loginedUser.getRoleIdList().contains(Long.valueOf(6L)))) {
			vo.setSuccess(false);
			vo.setMessage("没有权限进行该操作");
			return vo;
		}
		List<OrderForCheckVO> orders = null;
		//如果是学院管理员
		if (loginedUser.getRoleIdList().contains(Long.valueOf(5L))) {
			orders = this.studentFacade.queryOrdersForCheck(loginedUser.getCollegeId());
		} else {
			orders = this.studentFacade.queryOrdersForCheck();
		}
		
		vo.setMessage("请求成功！");
		vo.setData(orders);
		return vo;
	}
	
	@RequestMapping(value = "/queryForCheckOne.do", method = RequestMethod.POST)
	public @ResponseBody ResponseVO queryForCheckOne(@RequestBody OrderQueryVO input) {
		//先检查权限
		ResponseVO vo = new ResponseVO();
		UserVO loginedUser = this.contextManager.getUserVO();
		if (loginedUser == null) {
			vo.setSuccess(false);
			vo.setMessage("需要重新登录");
			return vo;
		}
		//检查角色是否为管理员角色
		if (!(loginedUser.getRoleIdList().contains(Long.valueOf(5L)) || loginedUser.getRoleIdList().contains(Long.valueOf(6L)))) {
			vo.setSuccess(false);
			vo.setMessage("没有权限进行该操作");
			return vo;
		}
		
		CompanyVO company = this.studentFacade.queryOrderForCheckOne(input.getId());
		System.out.println(company.getStudent().getName());
		
		vo.setMessage("请求成功！");
		vo.setData(company);
		return vo;
	}
	
	@RequestMapping(value = "/queryTimeLines.do", method = RequestMethod.POST)
	public @ResponseBody ResponseVO queryForCheckOne(@RequestBody TimeLineQueryInput input) {
		//先检查权限
		String studentId = null;
		ResponseVO vo = new ResponseVO();
		if (StringUtil.isNull(input.getStudentId())) {
			UserVO userVO = this.contextManager.getUserVO();
			studentId = userVO.getEmail();
		} else {
			studentId = input.getStudentId();
		}
		List<OrderHistoryVO> historys = this.studentFacade.queryTimeLines(studentId);
		List<String> timeLines = convert(historys);
		vo.setMessage("请求成功！");
		vo.setData(timeLines);
		return vo;
	}
	
	@RequestMapping(value = "/check.do", method = RequestMethod.POST)
	public @ResponseBody ResponseVO checkOrder(@RequestBody OrderCheckVO input) {
		//先检查权限
		ResponseVO vo = new ResponseVO();
		UserVO loginedUser = this.contextManager.getUserVO();
		if (loginedUser == null) {
			vo.setSuccess(false);
			vo.setMessage("需要重新登录");
			return vo;
		}
		//检查角色是否为管理员角色
		if (!(loginedUser.getRoleIdList().contains(Long.valueOf(5L)) || loginedUser.getRoleIdList().contains(Long.valueOf(6L)))) {
			vo.setSuccess(false);
			vo.setMessage("没有权限进行该操作");
			return vo;
		}
		this.studentFacade.checkOrder(input);
		vo.setMessage("请求成功！");
		vo.setData("审批成功！");
		return vo;
	}
	
	private List<String> convert(List<OrderHistoryVO> historys) {
		List<String> timelines = new ArrayList<String> ();
		for (OrderHistoryVO history : historys) {
			StringBuilder sb = new StringBuilder();
			sb.append(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(history.getCreateDate()));
			String statusDesc = this.configManager.getStatusDesc(history.getStatus());
			sb.append(" ").append(statusDesc);
			if (StringUtil.isNotEmpty(history.getRemark())) {
				sb.append(" 备注：").append(history.getRemark());
			}
			
			timelines.add(sb.toString());
		}
		return timelines;
	}
	
}
